package org.example.wx.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.mysql.cj.xdevapi.FilterParams;
import org.example.common.utils.DateTimeUtil;
import org.example.common.utils.ParamFilter;
import org.example.db.entity.Goods;
import org.example.db.entity.GoodsSpecification;
import org.example.db.entity.GoodsOrder;
import org.example.db.mapper.GoodsMapper;
import org.example.db.mapper.GoodsSpecificationMapper;
import org.example.db.mapper.OrderMapper;
import org.example.security.service.AuthService;
import org.example.wx.constant.OrderStatus;
import org.example.wx.dto.GoodsGeneralDto;
import org.example.wx.dto.GoodsOrderGeneralDto;
import org.example.wx.dto.OrderDto;
import org.example.wx.exception.ErrorDataException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;

@Service
public class WxOrderService {

    OrderMapper orderMapper;
    AuthService authService;
    GoodsSpecificationMapper goodsSpecificationMapper;
    GoodsMapper goodsMapper;

    public WxOrderService(OrderMapper orderMapper, AuthService authService, GoodsSpecificationMapper goodsSpecificationMapper, GoodsMapper goodsMapper) {
        this.orderMapper = orderMapper;
        this.authService = authService;
        this.goodsSpecificationMapper = goodsSpecificationMapper;
        this.goodsMapper = goodsMapper;
    }

    /**
     *
     * @param orderList goodsId 可以为null
     */
    @Transactional
    public void addOrder(List<OrderDto> orderList){

        if(orderList.size() > 20){
            throw new RuntimeException();
        }

        for(OrderDto orderDto: orderList){
            GoodsOrder goodsOrder = new GoodsOrder();
            GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectById(orderDto.getGoodsSpecId());

            if(orderDto.getGoodsId() != null){
                if(goodsSpecification == null || !goodsSpecification.getGoodsId().equals(orderDto.getGoodsId())){
                    throw new ErrorDataException("商品数据错误");
                }
            }

            goodsOrder.setWxUserId(authService.getWxUserId());

            if(orderDto.getGoodsId() == null){
                goodsOrder.setGoodsId(goodsSpecification.getGoodsId());
            }else{
                goodsOrder.setGoodsId(orderDto.getGoodsId());
            }
            goodsOrder.setGoodsSpecId(orderDto.getGoodsSpecId());
            goodsOrder.setOrderStatus(OrderStatus.Submitted.getType());
            goodsOrder.setGoodsPrice(goodsSpecification.getPrice());
            goodsOrder.setNumber(orderDto.getNumber());
            goodsOrder.setAddTime(DateTimeUtil.getDateTimeString());
            goodsOrder.setUpdateTime(DateTimeUtil.getDateTimeString());
            orderMapper.insert(goodsOrder);
        }

    }

    public List<GoodsOrderGeneralDto> list(String type){

        ParamFilter.orderTypeFilter(type);
        Long wxUserId = authService.getWxUserId();

        List<GoodsOrder> goodsOrders = orderMapper.selectList(
                Wrappers.lambdaQuery(GoodsOrder.class).eq(GoodsOrder::getWxUserId, wxUserId));

        LinkedList<GoodsOrderGeneralDto> goodsOrderGeneralDtoList = new LinkedList<>();
        if(type.equals(OrderStatus.Submitted.getTypeStr())){

            for(GoodsOrder goodsOrder: goodsOrders){

                if(OrderStatus.Submitted.getType().equals(goodsOrder.getOrderStatus())){
                    GoodsOrderGeneralDto goodsGeneralDto = this.getGoodsGeneralDto(goodsOrder);
                    goodsOrderGeneralDtoList.add(goodsGeneralDto);
                }
            }
        }else if(type.equals(OrderStatus.Done.getTypeStr())){
            for(GoodsOrder goodsOrder: goodsOrders){

                if(OrderStatus.Done.getType().equals(goodsOrder.getOrderStatus())){
                    GoodsOrderGeneralDto goodsGeneralDto = this.getGoodsGeneralDto(goodsOrder);
                    goodsOrderGeneralDtoList.add(goodsGeneralDto);
                }
            }
        }else{
            for(GoodsOrder goodsOrder: goodsOrders){

                GoodsOrderGeneralDto goodsGeneralDto = this.getGoodsGeneralDto(goodsOrder);
                goodsOrderGeneralDtoList.add(goodsGeneralDto);

            }
        }
        return goodsOrderGeneralDtoList;
    }

    public GoodsOrderGeneralDto getGoodsGeneralDto(GoodsOrder goodsOrder){

        Goods goods = goodsMapper.selectById(goodsOrder.getGoodsId());
        GoodsSpecification goodsSpecification = goodsSpecificationMapper.selectById(goodsOrder.getGoodsSpecId());
        GoodsOrderGeneralDto goodsOrderGeneralDto = new GoodsOrderGeneralDto();
        goodsOrderGeneralDto.setNumber(goodsOrder.getNumber());
        goodsOrderGeneralDto.setPrice(goodsOrder.getGoodsPrice());
        goodsOrderGeneralDto.setSpecificationValue(goodsSpecification.getValue());
        goodsOrderGeneralDto.setPictureUrl(goodsSpecification.getPictureUrl());
        goodsOrderGeneralDto.setSpecificationId(goodsOrder.getGoodsSpecId());
        goodsOrderGeneralDto.setGoodsName(goods.getName());
        goodsOrderGeneralDto.setOrderId(goodsOrder.getId());
        return goodsOrderGeneralDto;
    }

    public void confirm(Long orderId){

        orderMapper.update(null, Wrappers.lambdaUpdate(GoodsOrder.class)
                .eq(GoodsOrder::getId, orderId).set(GoodsOrder::getOrderStatus, OrderStatus.OK.getType()));
    }

}
